package cc.lzmsh.handler.exception;

import cc.lzmsh.domain.result.Result;
import cc.lzmsh.enums.AppHttpCodeEnum;
import cc.lzmsh.exception.SystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Author:   msh
 * Date:     2024/9/19 下午4:08
 * Description:
 */

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    // 捕获系统异常
    @ExceptionHandler(SystemException.class)
    public Result systemExceptionHandler(SystemException e) {
        // 获取异常信息
        log.error("出现了异常！{}", e);
        // 返回给前端
        return Result.error(e.getMsg(), e.getCode());
    }

    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e) {
        // 获取异常信息
        log.error("出现了异常！{}", e);
        // 返回给前端
        return Result.error(e.getMessage(),AppHttpCodeEnum.SYSTEM_ERROR.getCode());
    }
}
